home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-09-23 | 8.6 KB | 379 lines | [TEXT/MPS ] |
- /*------------------------------------------------------------------------------
- # MacTutorApp
- #
- # A rudimentary application skeleton
- # J. Langowski / MacTutor 1989
- # Added high-level event support / JL Sept. 1991
- #------------------------------------------------------------------------------*/
- #include <Types.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Controls.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <Scrap.h>
- #include <ToolUtils.h>
- #include <Memory.h>
- #include <SegLoad.h>
- #include <Files.h>
- #include <OSUtils.h>
- #include <Traps.h>
- #include <StdLib.h>
-
- #include <AppleEvents.h>
- #include <GestaltEqu.h>
-
- #include "TDocument.h"
- #include "TApplication.h"
- #include "MacTutor7App.h"
- #include "MacTutorDoc.h"
- #include "MacTutorGrow.h"
-
- const short kOSEvent = app4Evt;
-
- // Our application object, initialized in main(). We make it
- // global so our functions which don't belong to any class
- // can find the active document.
- TMacTutorApp *gTheApplication;
-
- /* Handlers for the requires AppleEvent suite */
-
- // Create a new document and window.
- void TMacTutorApp::DoNew(void)
- {
- TMacTutorGrow* tMacTutorDoc;
-
- tMacTutorDoc = new TMacTutorGrow(rDocWindow,"\pNothing selected yet.");
- // if we didn't get an allocation error, add it to list
- if (tMacTutorDoc != nil)
- fDocList->AddDoc(tMacTutorDoc);
- }
-
- // do the necessary things when you get an 'oapp' high level event
- void TMacTutorApp::DoOpen(void) { SysBeep(5); DoNew(); }
-
- // We don't print any documents
- void TMacTutorApp::DoPrint(void) { SysBeep(5); }
-
- void TMacTutorApp::Terminate(void) { ExitLoop(); }
-
- void TMacTutorApp::DoHighLevelEvent(void)
- {
- AEProcessAppleEvent(&fTheEvent);
- }
-
- void AEDoOpen(void) { gTheApplication->DoOpen(); }
- void AEDoNew(void) { gTheApplication->DoNew(); }
- void AEDoPrint(void) { gTheApplication->DoPrint(); }
- void AETerminate(void) { gTheApplication->Terminate(); }
-
- // initialize the application
- TMacTutorApp::TMacTutorApp(void)
- {
- Handle menuBar;
-
- // initialize Apple Event handlers
- OSErr err;
- long result;
- Boolean gHasAppleEvents;
-
-
- gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &result) ? false : result != 0);
-
- if (gHasAppleEvents) {
- err = AEInstallEventHandler(kCoreEventClass,
- kAEOpenApplication,
- (EventHandlerProcPtr) &AEDoOpen, 0L, false);
- err = AEInstallEventHandler(kCoreEventClass,
- kAEOpenDocuments,
- (EventHandlerProcPtr) &AEDoNew, 0L, false);
- err = AEInstallEventHandler(kCoreEventClass,
- kAEPrintDocuments,
- (EventHandlerProcPtr) &AEDoPrint, 0L, false);
- err = AEInstallEventHandler(kCoreEventClass,
- kAEQuitApplication,
- (EventHandlerProcPtr) &AETerminate, 0L, false);
- }
-
- // read menus into menu bar
- menuBar = GetNewMBar(rMenuBar);
- // install menus
- SetMenuBar(menuBar);
- DisposHandle(menuBar);
- // add DA names to Apple menu
- AddResMenu(GetMHandle(mApple), 'DRVR');
- DrawMenuBar();
-
- // create empty mouse region
- fMouseRgn = NewRgn();
- }
-
- // Tell TApplication class how much heap we need
- long TMacTutorApp::HeapNeeded(void)
- {
- return (kMinSize * 1024);
- }
-
- // Calculate a sleep value for WaitNextEvent. This takes into account the things
- // that DoIdle does with idle time.
-
- unsigned long TMacTutorApp::SleepVal(void)
- {
- unsigned long sleep;
- const long kSleepTime = 0x7fffffff; // a very large positive number
-
- sleep = kSleepTime; // default value for sleep
- if ((!fInBackground))
- {
- sleep = GetCaretTime(); // A reasonable time interval for MenuClocks, etc.
- }
- return sleep;
- }
-
- void TMacTutorApp::AdjustMenus(void)
- {
- WindowPtr frontmost;
- MenuHandle menu;
- Boolean undo,cutCopyClear,paste;
-
- TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
-
- frontmost = FrontWindow();
-
- menu = GetMHandle(mFile);
- if ( fDocList->NumDocs() < kMaxOpenDocuments )
- EnableItem(menu, iNew); // New is enabled when we can open more documents
- else DisableItem(menu, iNew);
- if ( frontmost != (WindowPtr) nil ) // Close is enabled when there is a window to close
- EnableItem(menu, iClose);
- else DisableItem(menu, iClose);
-
- undo = false;
- cutCopyClear = false;
- paste = false;
-
- if ( fMacTutorCurDoc == nil )
- {
- undo = true; // all editing is enabled for DA windows
- cutCopyClear = true;
- paste = true;
- }
-
- menu = GetMHandle(mEdit);
- if ( undo )
- EnableItem(menu, iUndo);
- else
- DisableItem(menu, iUndo);
-
- if ( cutCopyClear )
- {
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- }
- else
- {
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iClear);
- }
-
- if ( paste )
- EnableItem(menu, iPaste);
- else
- DisableItem(menu, iPaste);
-
- menu = GetMHandle(myMenu);
- EnableItem(menu, item1);
- EnableItem(menu, item2);
- EnableItem(menu, item3);
- EnableItem(menu, item5);
-
- CheckItem(menu, item1, false);
- CheckItem(menu, item2, false);
- CheckItem(menu, item3, false);
- CheckItem(menu, item5, false);
- CheckItem(menu, fMacTutorCurDoc->GetItemSelected(), true);
- } // AdjustMenus
-
- void TMacTutorApp::DoMenuCommand(short menuID, short menuItem)
- {
- short itemHit;
- Str255 daName;
- short daRefNum;
- WindowPtr window;
- TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
-
- window = FrontWindow();
- switch ( menuID )
- {
- case mApple:
- switch ( menuItem )
- {
- case iAbout: // About box
- itemHit = Alert(rAboutAlert, nil);
- break;
- default: // DAs etc.
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch ( menuItem )
- {
- case iNew:
- DoNew();
- break;
- case iClose:
- if (fMacTutorCurDoc != nil)
- {
- fDocList->RemoveDoc(fMacTutorCurDoc);
- delete fMacTutorCurDoc;
- }
- else CloseDeskAcc(((WindowPeek) fWhichWindow)->windowKind);
- break;
- case iQuit:
- Terminate();
- break;
- }
- break;
- case mEdit: // call SystemEdit for DA editing & MultiFinder
- if ( !SystemEdit(menuItem-1) )
- {
- switch ( menuItem )
- {
- case iCut:
- break;
- case iCopy:
- break;
- case iPaste:
- break;
- case iClear:
- break;
- }
- }
- break;
- case myMenu:
- if (fMacTutorCurDoc != nil)
- {
- switch ( menuItem )
- {
- case item1:
- fMacTutorCurDoc->SetDisplayString("\pC++");
- break;
- case item2:
- fMacTutorCurDoc->SetDisplayString("\pSample");
- break;
- case item3:
- fMacTutorCurDoc->SetDisplayString("\pApplication");
- break;
- case item5:
- fMacTutorCurDoc->SetDisplayString("\pHave Fun");
- break;
- }
- fMacTutorCurDoc->SetItemSelected(menuItem);
- InvalRect(&(window->portRect));
- fMacTutorCurDoc->DoUpdate();
- }
- break;
- }
- HiliteMenu(0);
- } // DoMenuCommand
-
-
- void TMacTutorApp::EventLoop(void)
- // overridden to accommodate high level events
- {
- int gotEvent;
- EventRecord tEvt;
-
- SetUp(); // call setup routine
- DoIdle(); // do idle once
-
- while (fDone == false)
- {
- // always set up fWhichWindow before doing anything
- fWhichWindow = FrontWindow();
- // see if window belongs to a document
- fCurDoc = fDocList->FindDoc(fWhichWindow);
- // make sure we always draw into correct window
- SetPort(fWhichWindow);
-
- DoIdle(); // call idle time handler
-
- if (fHaveWaitNextEvent)
- {
- gotEvent = WaitNextEvent(everyEvent, &tEvt, SleepVal(), fMouseRgn);
- }
- else
- {
- SystemTask();
- gotEvent = GetNextEvent(everyEvent, &tEvt);
- }
- fTheEvent = tEvt;
-
- // make sure we got a real event
- if ( gotEvent )
- {
- AdjustCursor();
- switch (fTheEvent.what)
- {
- case mouseDown :
- DoMouseDown();
- break;
- case mouseUp :
- DoMouseUp();
- break;
- case keyDown :
- case autoKey :
- DoKeyDown();
- break;
- case updateEvt :
- DoUpdateEvt();
- break;
- case diskEvt :
- DoDiskEvt();
- break;
- case activateEvt :
- DoActivateEvt();
- break;
- case kHighLevelEvent : // added Sept 91
- DoHighLevelEvent(); // J.L.
- break; //
- case kOSEvent :
- DoOSEvent();
- break;
- default :
- break;
- } // end switch (fTheEvent.what)
- }
- AdjustCursor();
- }
- // call cleanup handler
- CleanUp();
- }
-
-
- // main is the entrypoint to the program
- int main(void)
- {
- // Create our application object. This MUST be the FIRST thing
- // done in main(), since it initializes the Toolbox for us.
- gTheApplication = new TMacTutorApp;
- if (gTheApplication == nil) // if we couldn't allocate object (impossible!?)
- return 0; // go back to Finder
-
- // Start our main event loop running. This won't return until user quits
- gTheApplication->EventLoop();
-
- // We always return a value, like good little ANSI worshippers
- return 0;
- }
-
-